import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* 布局 */
import Layout from '@/layout'

/* 抽离的路由模块 */
import dashboardRouter from './modules/dashboard.js'
import systemRouter from './modules/system.js'
// import permissionRouter from './modules/permission.js'
import documentationRouter from './modules/documentation.js'
import iconRouter from './modules/icon.js'
import componentsRouter from './modules/components'
import chartsRouter from './modules/charts'
import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   如果设置为 true，项目将不会显示在侧边栏中 (默认为 false)
 * alwaysShow: true               如果设置为 true，将始终显示根菜单
 *                                如果没有设置 alwaysShow，当 item 有多于一个子路由时，
 *                                它将变成嵌套模式，否则不显示根菜单
 * path: '/redirect'              路由路径
 * redirect: noRedirect           如果设置了noRedirect，则不会在breadcrumb中重定向
 * name:'router-name'             该路由的名称 <keep-alive> (必须设置!!!)
 * meta : {
    roles: ['admin','editor']    控制页面角色(可以设置多个角色)
    title: 'title'               名称显示在侧边栏和面包屑中(推荐设置)
    icon: 'svg-name'/'el-icon-x' 在侧边栏中显示图标
    noCache: true                如果设置为true，页面将不会被缓存(默认为 false)
    affix: true                  如果设置为true，标签将在标签视图中添加
    breadcrumb: false            如果设置为false，则条目将隐藏在breadcrumb中(默认为true) if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  如果设置了路径，侧边栏将突出显示您设置的路径
  }
 */

/**
 * constantRoutes
 * 没有权限要求的页面
 * 所有角色都可以访问
 */
export const constantRoutes = [{
  path: '/redirect',
  component: Layout,
  hidden: true,
  children: [{
    path: '/redirect/:path(.*)',
    component: () => import('@/views/redirect/index.vue')
  }]
}, {
  path: '/login',
  component: () => import('@/views/login/index.vue'),
  hidden: true
}, {
  path: '/auth-redirect',
  component: () => import('@/views/login/auth-redirect'),
  hidden: true
}, {
  path: '/404',
  component: () => import('@/views/error-page/404'),
  hidden: true
}, {
  path: '/401',
  component: () => import('@/views/error-page/401'),
  hidden: true
}, {
  path: '/profile',
  component: Layout,
  redirect: '/profile/index',
  hidden: true,
  children: [{
    path: 'index',
    component: () => import('@/views/profile/index'),
    name: 'Profile',
    meta: { title: '个人中心', icon: 'user', noCache: true }
  }]
}]

/**
 * 异步路由
 * 需要根据用户角色动态加载的路由
 */
export const asyncRoutes = [
  // 首页路由
  dashboardRouter,
  // 系统设置
  systemRouter,
  // 文档路由
  documentationRouter,
  // 图标路由
  iconRouter,
  /** 当你的路由图太长，你可以把它分成小模块 **/
  componentsRouter,
  chartsRouter,
  nestedRouter,
  {
    path: '/error',
    component: Layout,
    redirect: 'noRedirect',
    name: 'ErrorPages',
    meta: {
      title: '错误页面',
      icon: '404'
    },
    children: [
      {
        path: '401',
        component: () => import('@/views/error-page/401'),
        name: 'Page401',
        meta: { title: '401', noCache: true }
      },
      {
        path: '404',
        component: () => import('@/views/error-page/404'),
        name: 'Page404',
        meta: { title: '404', noCache: true }
      }
    ]
  },
  {
    path: '/theme',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/theme/index'),
        name: 'Theme',
        meta: { title: '主题', icon: 'theme' }
      }
    ]
  },
  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://cn.bing.com/images/search?q=%e7%be%8e%e5%a5%b3%e5%9b%be%e7%89%87&qpvt=%e7%be%8e%e5%a5%b3%e5%9b%be%e7%89%87&form=IGRE&first=1',
        meta: { title: '外部链接', icon: 'link' }
      }
    ]
  },
  // 404页一定要放在最后！！
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // 请求服务支持
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重置路由器
}

export default router
